<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="名称" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="字段名称" prop="filedName">
        <el-input
          v-model="queryParams.filedName"
          placeholder="请输入字段名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警内容" prop="alarmRemark">
        <el-input
          v-model="queryParams.alarmRemark"
          placeholder="请输入报警内容"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="报警开关" prop="alarmSwitch">
        <el-select v-model="queryParams.alarmSwitch" placeholder="请选择报警开关" clearable size="small">
          <el-option
            v-for="dict in alarmSwitchOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['basic:measurpoint:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['basic:measurpoint:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['basic:measurpoint:remove']"
        >删除</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['basic:measurpoint:export']"
        >导出</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="measurpointList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="id" />-->
      <el-table-column label="名称" align="center" prop="name" />
      <el-table-column label="字段名称" align="center" prop="filedName" />
      <el-table-column label="报警内容" align="center" prop="alarmRemark" />
      <el-table-column label="报警开关" align="center" prop="alarmSwitch" :formatter="alarmSwitchFormat" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['basic:measurpoint:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['basic:measurpoint:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改测点信息对话框 -->
    <el-dialog v-myFullscreen :title="title" :visible.sync="open" width="900px" append-to-body  v-myDialogDrag>
      <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
         <!-- <el-col :span="12">
            <el-form-item label="编号" prop="code">
              <el-input v-model="form.code" placeholder="请输入编号" :disabled="openDisabled" />
            </el-form-item>
          </el-col>-->
          <el-col :span="12">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name" placeholder="请输入名称" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="字段名称" prop="filedName">
              <el-input v-model="form.filedName" placeholder="请输入字段名称" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="所属设备" prop="deviceid">
              <Dialog
              @dialogChange="form.deviceid = $event.value"
              :echoValue="form.devicename"
              parameter="rows"
              value="id"
              label="name"
              search="code/name"
              url="/basic/eqpInfo/list"
              :column="['设备编号/code', '设备名称/name']"
              :openDisabled="openDisabled"
              placeholder="请选择内容"
              />
            </el-form-item>
          </el-col>
        <!--  <el-col :span="12">
            <el-form-item label="所属工厂" prop="deptId">
              <el-input v-model="form.deptId" placeholder="请输入所属工厂" :disabled="openDisabled" />
            </el-form-item>
          </el-col>-->

          <el-col :span="12">
            <el-form-item label="报警数据类型" prop="alarmType" >
              <el-select v-model="form.alarmType" placeholder="请选择报警数据类型" :disabled="openDisabled" @change="changealarmType">
                <el-option
                  v-for="dict in alarmTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警内容" prop="alarmRemark">
              <el-input v-model="form.alarmRemark" placeholder="请输入报警内容" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="报警开关">
              <el-radio-group v-model="form.alarmSwitch" :disabled="openDisabled" @change="changeHandler">
                <el-radio
                  v-for="dict in alarmSwitchOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="生成维修记录">
              <el-radio-group v-model="form.isRepair" :disabled="openDisabled">
                <el-radio
                  v-for="dict in isRepairOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="保存停机记录">
              <el-radio-group v-model="form.isSave" :disabled="openDisabled">
                <el-radio
                  v-for="dict in isSaveOptions"
                  :key="dict.dictValue"
                  :label="dict.dictValue"
                >{{dict.dictLabel}}</el-radio>
              </el-radio-group>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="运行开标志" prop="longName">
              <el-input v-model="form.runState" title="请输入停机标识，多个用逗号隔开" placeholder="请输入开机标识，如：-false，多个用‘-’隔开，如：-1-3-4" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="维修类型" prop="repairType">
              <el-select v-model="form.repairType" placeholder="请选择维修类型" :disabled="openDisabled">
                <el-option
                  v-for="dict in repairTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="停机类型" prop="stopType">
              <el-select v-model="form.stopType" placeholder="请选择停机类型" :disabled="openDisabled">
                <el-option
                  v-for="dict in stopTypeOptions"
                  :key="dict.dictValue"
                  :label="dict.dictLabel"
                  :value="dict.dictValue"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <div v-if="isalopen">


            <div v-if="flag">
              <el-col :span="12">
                <el-form-item label="低低限" prop="llowalarm">
                  <el-input v-model="form.llowalarm" placeholder="请输入低低限" :disabled="openDisabled" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="低限" prop="lowalarm">
                  <el-input v-model="form.lowalarm" placeholder="请输入低限" :disabled="openDisabled" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="高限" prop="highalarm">
                  <el-input v-model="form.highalarm" placeholder="请输入高限" :disabled="openDisabled" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="高高限" prop="hhighalarm">
                  <el-input v-model="form.hhighalarm" placeholder="请输入高高限" :disabled="openDisabled" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="变化率报警限值" prop="rate">
                  <el-input v-model="form.rate" placeholder="请输入变化率报警限值" :disabled="openDisabled" />
                </el-form-item>
              </el-col>
              <el-col :span="12">
                <el-form-item label="变化率周期" prop="rateperiod">
                  <el-input v-model="form.rateperiod" placeholder="请输入变化率周期" :disabled="openDisabled" />
                </el-form-item>
              </el-col>
            </div>
          </div>


         <!-- <el-col :span="12">
            <el-form-item label="长名称" prop="longName">
              <el-input v-model="form.longName" placeholder="请输入长名称" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点链接" prop="taglink">
              <el-input v-model="form.taglink" placeholder="请输入点链接" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="通道名" prop="channel">
              <el-input v-model="form.channel" placeholder="请输入通道名" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="设备名" prop="device">
              <el-input v-model="form.device" placeholder="请输入设备名" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点类型" prop="tagtype">
              <el-select v-model="form.tagtype" placeholder="请选择点类型" :disabled="openDisabled">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="数据类型" prop="datatype">
              <el-select v-model="form.datatype" placeholder="请选择数据类型" :disabled="openDisabled">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="量程上限" prop="rangemaximum">
              <el-input v-model="form.rangemaximum" placeholder="请输入量程上限" :disabled="openDisabled" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="PLC数据类型" prop="plcDatatype">
              <el-select v-model="form.plcDatatype" placeholder="请选择PLC数据类型" :disabled="openDisabled">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="初始值" prop="initvalue">
              <el-input v-model="form.initvalue" placeholder="请输入初始值" :disabled="openDisabled" />
            </el-form-item>
          </el-col>-->
          <el-col :span="24">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" :disabled="openDisabled" />
            </el-form-item>
          </el-col>


        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer"  v-if="!openDisabled">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
  import { listMeasurpoint, getMeasurpoint, delMeasurpoint, addMeasurpoint, updateMeasurpoint } from "@/api/basic/measurpoint/measurpoint";

  export default {
    name: "Measurpoint",
    data() {
      return {
        //弹窗是否禁止编辑
        openDisabled: true,
        //是否显示模拟量编辑变量
        flag:true,
        //是否报警
        isalopen:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 测点信息表格数据
        measurpointList: [],
        // 弹出层标题
        title: "",
        // 报警数据类型字典
        alarmTypeOptions: [],
        // 是否显示弹出层
        open: false,
        // 报警开关字典
        alarmSwitchOptions: [],
        // 生成维修记录字典
        isRepairOptions: [],
        // 保存停机记录字典
        isSaveOptions: [],
        // 维修类型字典
        repairTypeOptions: [],
        // 停机类型字典
        stopTypeOptions: [],
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          name: null,
          filedName: null,
          alarmRemark: null,
          alarmSwitch: null,
        },
        // 表单参数
        form: {},
        // 表单校验
        rules: {
        }
      };
    },
    created() {
      this.getList();
      this.getDicts("sys_yes_no").then(response => {
        this.alarmSwitchOptions = response.data;
      });
      this.getDicts("sys_yes_no").then(response => {
        this.isRepairOptions = response.data;
      });
      this.getDicts("sys_yes_no").then(response => {
        this.isSaveOptions = response.data;
      });
      this.getDicts("fault_type").then(response => {
        this.repairTypeOptions = response.data;
      });
      this.getDicts("alarm_stop_type").then(response => {
        this.stopTypeOptions = response.data;
      });
      this.getDicts("basic_measurpoint_type").then(response => {
        this.alarmTypeOptions = response.data;
      });
    },
    methods: {
      /** 报警开关变化 */
      changeHandler(value) {

        if(value=="Y")
        {//判断是是否打开报警
          this.isalopen=true;
        }else{
          this.isalopen=false;
        }

      },
      /** 报警变量类型选择变化 */
      changealarmType(value) {
        console.log('改变之后的值是:' + value)
        if(value==0)
        {//判断是模拟量还是开关量0：模拟量，1：开关量。
          this.flag=true;
        }else{
          this.flag=false;
        }

      },

      /** 查询测点信息列表 */
      getList() {
        this.loading = true;
        listMeasurpoint(this.queryParams).then(response => {
          this.measurpointList = response.rows;
          this.total = response.total;
          this.loading = false;
        });
      },
      // 报警开关字典翻译
      alarmSwitchFormat(row, column) {
        return this.selectDictLabel(this.alarmSwitchOptions, row.alarmSwitch);
      },
      // 生成维修记录字典翻译
      isRepairFormat(row, column) {
        return this.selectDictLabel(this.isRepairOptions, row.isRepair);
      },
      // 保存停机记录字典翻译
      isSaveFormat(row, column) {
        return this.selectDictLabel(this.isSaveOptions, row.isSave);
      },
      // 维修类型字典翻译
      repairTypeFormat(row, column) {
        return this.selectDictLabel(this.repairTypeOptions, row.repairType);
      },
      // 停机类型字典翻译
      stopTypeFormat(row, column) {
        return this.selectDictLabel(this.stopTypeOptions, row.stopType);
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: null,
          code: null,
          name: null,
          filedName: null,
          deviceid: null,
          devicename: null,
          deptId: null,
          llowalarm: null,
          lowalarm: null,
          highalarm: null,
          hhighalarm: null,
          rate: null,
          rateperiod: null,
          longName: null,
          taglink: null,
          channel: null,
          device: null,
          tagtype: null,
          datatype: null,
          rangemaximum: null,
          plcDatatype: null,
          initvalue: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null,
          remark: null,
          delFlag: null,
          alarmType: null,
          alarmRemark: null,
          alarmSwitch: "0",
          isRepair: "0",
          isSave: "0",
          repairType: null,
          runState: null,
          stopType: null
        };
        this.resetForm("form");
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm");
        this.handleQuery();
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!==1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.openDisabled = false;
        this.open = true;
        this.title = "添加测点信息";
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        this.openDisabled = false;
        const id = row.id || this.ids
        if(row.alarmType==0)
        {//判断是模拟量还是开关量0：模拟量，1：开关量。
          this.flag=true;
        }else{
          this.flag=false;
        }
          if(row.alarmSwitch=="Y")
          {//判断是是否打开报警
            this.isalopen=true;
          }else{
            this.isalopen=false;
          }
        getMeasurpoint(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改测点信息";
        });
      },
      /** 查看按钮 */
      handleView(row) {
        this.reset();
        this.openDisabled = true;
        const id = row.id || this.ids
        if(row.alarmType==0)
        {//判断是模拟量还是开关量0：模拟量，1：开关量。
          this.flag=true;
        }else{
          this.flag=false;
        }
        if(row.alarmSwitch=="Y")
        {//判断是是否打开报警
          this.isalopen=true;
        }else{
          this.isalopen=false;
        }
        getMeasurpoint(id).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "查看测点信息";
        });
      },
      /** 提交按钮 */
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != null) {
              updateMeasurpoint(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              const rLoading = this.openLoading();
              addMeasurpoint(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                rLoading.close();
                this.getList();
              }).catch((error) => {
                rLoading.close();
              });
            }
          }
        });
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const ids = row.id || this.ids;
        this.$confirm('是否确认删除测点信息编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delMeasurpoint(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 导出按钮操作 */
      handleExport() {
        this.download('basic/measurpoint/export', {
          ...this.queryParams
        }, '测点信息'+this.parseTime(new Date)+'.xlsx')
      }
    }
  };
</script>
